<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class TransferSet extends Base
{
    public function initialize()
    {
        parent::initialize();
        //钱包
        View::assign('wallet',logic('Wallet')->getName());

        View::assign('totype',['转给其他人','转给自己']);
    }
    public function index(){
        //$map = $this->_search();
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            $count = Db::name('transfer_set')->alias('t')->where($map)->count();

            $field = 't.*';
            $list = Db::name('transfer_set')
                ->alias('t')
                ->field($field)
                ->where($map)
                ->order('t.id desc ')
                ->page($page,$limit)
                ->select();
            foreach($list as $k=>$v){
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        return View::fetch();
    }

    public function edit(){
        $wallet_arr = logic('Wallet')->getName();
        View::assign('wallet_arr',$wallet_arr);
        $id = input('id');
        $info = Db::name('transfer_set')->where('id',$id)->find();
        View::assign('info',$info);
        return View::fetch();
    }
    public function save(){
        $params = input();
        if($params['tome']==1 && $params['from']==$params['to']){
            return json(['status'=>0,'msg'=>'转给自己时，转出货币不能和转入货币一样']);
        }
        $data = [];
        $data['title'] = $params['title'];
        $data['from'] = $params['from'];
        $data['to'] = $params['to'];
        $data['tome'] = $params['tome'];
        $data['fee'] = $params['fee'];
        $data['fee_type'] = $params['fee_type'];
        $data['changebl'] = $params['changebl'];
        $data['max'] = $params['max'];
        $data['min'] = $params['min'];
        $data['status'] = $params['status'];
        $data['w_time'] = time();
        if(!empty($params['id'])){
            Db::name('transfer_set')->where('id',$params['id'])->update($data);
        }else{
            Db::name('transfer_set')->insert($data);
        }
        return json(['status'=>1,'msg'=>'操作成功']);
    }
    public function del(){
        $params = input();
        Db::name('transfer_set')->where('id',$params['id'])->delete();
        return json(['status'=>1,'msg'=>'操作成功']);
    }

}